import { EventKey } from "../common/Key";

export const Partner = {
    EVENT_INIT_SUCC: "EVENT_INIT_SUCC",
    EVENT_INIT_FAIL: "EVENT_INIT_FAIL",

    // 初始化
    init(param, cb) {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.init(param, cb);
        } else {
            G.utils.scheduleOnce(() => {
                cb && cb({ event: this.EVENT_INIT_SUCC, param: {} });
            }, 0);
        }

        G.onFire.on(EventKey.EVENT_ON_SHOW, () => {
            this.shareCb && this.shareCb();
            this.shareCb = null;
        }, this);
    },

    createGameClub() {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.createGameClub();
        }
    },

    showGameClub() {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.showGameClub();
        }
    },

    hideGameClub() {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.hideGameClub();
        }
    },

    share(param, cb) {
        this.shareCb = cb;
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.share(param);
        } else {
            this.shareCb && this.shareCb();
            this.shareCb = null;
        }
    },
    postMessage(k, v) {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.postMessage(k, v);
        }
    },

    setUserCloudStorage(k, v, cb) {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.setUserCloudStorage(k, v, cb);
        }
    },

    navigateToMiniProgram(appid) {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            PartnerHelper.navigateToMiniProgram(appid);
        }
    },

    onShow(res) {
        G.onFire.fire(EventKey.EVENT_ON_SHOW);
    },
    onHide(res) {
        G.onFire.fire(EventKey.EVENT_ON_HIDE);
    },

    vibrateShort() {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            let isMute = G.audio.getIsVibrateMute();
            if (isMute) {
                return;
            }
            PartnerHelper.vibrateShort();
        }
    },
    vibrateLong() {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            let isMute = G.audio.getIsVibrateMute();
            if (isMute) {
                return;
            }
            PartnerHelper.vibrateLong();
        }
    },

    sendEvent(event, param) {
        if (window.wx && window.wx.aldSendEvent) {
            wx.aldSendEvent(event, param);
        }
    }
}